//
//  LPActivityDetailCommiuncationTableViewCell.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 2017/6/6.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPActivityDetailCommiuncationTableViewCell.h"
#import "LPActivityDetailTableFooterView.h"
#import "LPActivityDetailTableHeaderView.h"
#import "LPActivityDetailCommiuncationDetailTableViewCell.h"
@interface LPActivityDetailCommiuncationTableViewCell ()<UITableViewDelegate,UITableViewDataSource,LPActivityDetailFooterDelegate>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;

@end

@implementation LPActivityDetailCommiuncationTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}


- (void)setModelArray:(NSArray *)modelArray{
    _modelArray = modelArray;
    [self layoutUI];
    
}


#pragma mark - 界面
- (void)layoutUI{
    [self addSubview:self.tableView];
    [self addFooterAndHeader];
}


#pragma mark - 私有方法
- (void)addFooterAndHeader{
    LPActivityDetailTableHeaderView *heaer = [[LPActivityDetailTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 50 * [UIScreen zj_screenHScale]) buttonTitle:@"交流" buttonImgae:@"wxy_jiaoliu_l"];
    self.tableView.tableHeaderView = heaer;
    
    LPActivityDetailTableFooterView *footer = [[LPActivityDetailTableFooterView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 60 * [UIScreen zj_screenHScale])];
    footer.titles = @[@"查看更多交流"];
    footer.delegate = self;
    self.tableView.tableFooterView = footer;
    
}

#pragma mark - LPActivityDetailFooterDelegate
- (void)lpActivityFooter_clickButton_index:(NSInteger)index{
    /**  查看更多的点击方法  */
    if (self.delegate) {
        [self.delegate LPActivityDetailCommiuncationTabelWithCell:self watchMore:YES];
    }
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.modelArray.count == 0) {
        return 1;
    }else{
        return self.modelArray.count;
    }
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.modelArray.count == 0) {
        return 60 * [UIScreen zj_screenHScale];
    }else{
        LPActivityDetailCommiuncationDetailModel *model = self.modelArray[indexPath.row];
        return 80 * [UIScreen zj_screenHScale] + model.HEIGHT;
    }
    
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    LPActivityDetailCommiuncationDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
    if (!cell) {
        cell = [[LPActivityDetailCommiuncationDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
    }
    if (self.modelArray.count != 0) {
        cell.model = self.modelArray[indexPath.row];
    }else{
        cell.isNoData = YES;
    }
    
    return cell;
}


#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        
        if (self.modelArray.count == 0) {
            _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 110 * [UIScreen zj_screenHScale] + 60  * [UIScreen zj_screenHScale]) style:UITableViewStylePlain];
            
        }else{
            CGFloat height = 110 * [UIScreen zj_screenHScale];
            for (LPActivityDetailCommiuncationDetailModel *model  in self.modelArray) {
                height += 80 * [UIScreen zj_screenHScale] + model.HEIGHT;
            }

            _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], height) style:UITableViewStylePlain];
            
        }
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}

@end
